
indexApp
	.controller(
		'anaTaskCreateManageCtrl',
			function($scope, $http, $timeout, $interval) {

                $scope.taskConfig = {
                    taskId: null,
                    taskName: "",
                    createTime: null,
                    taskDesc: ""
                };

                $scope.taskList = [];
                $scope.focusTask = null;

                $scope.createTask = function() {

                    if ($scope.taskConfig.taskName.trim() === "") {
                        Swal("任务名不能为空！");
                    } else {
                        $http({
                            method : 'POST',
                            headers : {
                                'Content-Type' : 'application/json',
                            },
                            url : "analysis/task/create",
                            data : $scope.taskConfig
                        }).then(function(resp, status) {
                            $scope.taskConfig.taskId = resp.data.taskId;
                            console.log(resp.data);
                            $scope.status = status;
                            Swal("创建成功");

                            document.cookie="taskId="+$scope.taskConfig.taskId;
                            $interval(function () {
                                location.href = "ana-dataset.html";
                            }, 1000);

                        }, function(resp, status) {
                            $scope.resp = resp;
                            $scope.status = status;
                        });
                    }

                };


                /**
                 * 列出所有任务
                 */
                $scope.getAllTasks = function () {
                    console.log("[INFO] You are getting tasks");
                    // 任务输出隐藏
                    $scope.taskOuputDivVisible = false;

                    $http({
                        method: 'GET',
                        url: 'analysis/task/tasks',
                    }).then(function (resp, status) {
                        $scope.taskList = resp.data;

                        console.log("[INFO] Task list includes: ");
                        console.log(resp.data);
                        $scope.status = status;
                    }, function (resp, status) {
                        $scope.resp = resp;
                        $scope.status = status;
                    });
                };


                /**
                 * 删除任务
                 *
                 */
                $scope.delTask = function (task) {
                    console.log("[DELETE TASK] You are deletting the task whose id is "
                        + task.taskId);
                    swal({
                        title: '确认删除该任务',
                        type: 'warning',
                        showCancelButton: true,
                        confirmButtonColor: '#3085d6',
                        cancelButtonColor: '#d33',
                        confirmButtonText: '删除',
                        cancelButtonText: '取消'
                    }).then(function(isConfirm) {
                        console.log("ic",isConfirm);
                        if (isConfirm.value) {
                            $http(
                                {
                                    method: 'POST',
                                    url: 'analysis/task/delete/' + task.taskId
                                }).then(function (resp, status) {
                                $scope.getAllTasks();
                            }, function (resp, status) {
                                $scope.getAllTasks();
                            });
                            $http(
                                {
                                    method: 'POST',
                                    url: "http://127.0.0.1:3000/deletmodel",
                                    data:task
                                }).then(function (resp, status) {
                                $scope.getAllTasks();
                            }, function (resp, status) {
                                $scope.getAllTasks();
                            });
                            Swal.fire("删除成功");
                        }
                    })


                };



                $scope.showTaskDetail = function (task) {
                    $scope.focusTask = task;
                }

			});
